package com.duobaoyu.dby.supply.biz.util;

import com.alibaba.fastjson.JSON;
import com.duobaoyu.dby.common.core.constant.CommonConstants;
import com.duobaoyu.dby.common.core.constant.StringPool;
import com.duobaoyu.dby.common.core.util.R;
import com.duobaoyu.dby.supply.biz.exception.SupplyChainBizException;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.Objects;
import java.util.stream.Collectors;


@Slf4j
@UtilityClass
public class RemoteResultCheckUtil {
	/**
	 * @param u          返回结果
	 * @param serverName 服务名称
	 * @param t          请求参数
	 * @param <T>
	 * @param <U>
	 */
	@SafeVarargs
	public <T, U extends R<?>> void checkRemoteResult(U u, String serverName, T... t) {
		if(log.isDebugEnabled()) {
			log.debug("调用{}服务{}接口请求参数={},响应结果={}", serverName, Thread.currentThread().getStackTrace()[CommonConstants.TWO].getMethodName()
					, Arrays.stream(t).map(JSON::toJSONString).collect(Collectors.joining(StringPool.COMMA)), JSON.toJSONString(u));
		}
		if (!Objects.equals(CommonConstants.SUCCESS, u.getCode())) {
			log.error("调用{}服务{}接口请求参数={},响应结果={}", serverName, Thread.currentThread().getStackTrace()[CommonConstants.TWO].getMethodName()
					, Arrays.stream(t).map(JSON::toJSONString).collect(Collectors.joining(StringPool.COMMA)), JSON.toJSONString(u));
			SupplyChainBizException.fail(u.getCode(), u.getMsg());
		}
	}
}
